<template>
  <el-dialog
    :title="type == 'add' ? '添加' : '修改'"
    v-model="visible"
    width="400px"
    fullscreen
  >
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="formRef"
      label-width="100px"
      class="good-form"
    >
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>基本信息</span>
          </div>
        </template>
        <el-form-item label="作品名称" prop="workName">
          <el-input type="text" v-model="ruleForm.workName"></el-input>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <el-upload
            class="avatar-uploader"
            action="https://www.watabe-wedding.com.cn/mini/api/upload/img"
            accept="jpg,jpeg,png"
            :show-file-list="false"
            :before-upload="handleBeforeUpload"
            :on-success="handleUrlSuccess"
          >
            <img
              style="width: 100px; height: 100px; border: 1px solid #e9e9e9"
              v-if="ruleForm.cover"
              :src="ruleForm.cover"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input type="text" v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="ruleForm.description"></el-input>
        </el-form-item>
        <el-form-item label="内页标题" prop="title">
          <el-input type="text" v-model="ruleForm.title_info"></el-input>
        </el-form-item>
        <el-form-item label="内页描述" prop="description">
          <el-input
            type="textarea"
            v-model="ruleForm.description_info"
          ></el-input>
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="ruleForm.date"
            valueFormat="YYYY-MM-DD"
            format="YYYY-MM-DD"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="上级分类" prop="parentId">
          <el-cascader
            v-model="ruleForm.parentId"
            :options="options"
            placeholder="上级分类"
            clearable
            :props="cascaderProps"
            style="width: 200px; margin-right: 10px"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="背景图" prop="bg">
          <el-upload
            action="https://www.watabe-wedding.com.cn/mini/api/upload/img"
            list-type="picture-card"
            :before-upload="handleBeforeUpload"
            :show-file-list="false"
            :on-success="(e) => handleUrlsSuccess(e, 'bg')"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <div v-if="ruleForm.bg.length" style="display: flex; flex-wrap: wrap">
            <div
              v-for="(item, index) in ruleForm.bg"
              :key="item"
              style="display: flex; flex-direction: column; align-items: center"
            >
              <el-image
                style="
                  width: 120px;
                  height: 120px;
                  border: 1px solid #e9e9e9;
                  padding: 8px;
                "
                :src="item"
                class="avatar"
                :preview-src-list="ruleForm.bg"
              >
              </el-image>
              <el-button
                @click="handleRemove(index, 'bg')"
                style="margin: 8px"
                type="primary"
                icon="el-icon-delete"
              ></el-button>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="排序值" prop="order">
          <el-input-number
            size="medium"
            v-model="ruleForm.order"
            :min="1"
            :max="999"
          ></el-input-number>
        </el-form-item>
      </el-card>
      <br />
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>婚礼视频</span>
          </div>
        </template>
        <el-form-item label="标题" prop="video_title">
          <el-input type="text" v-model="ruleForm.video_title"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="video_description">
          <el-input
            type="textarea"
            v-model="ruleForm.video_description"
          ></el-input>
        </el-form-item>
        <el-form-item label="视频" prop="video">
          <el-upload
            class="upload-demo"
            :before-upload="handleVideoBeforeUpload"
            :show-file-list="false"
            :on-success="handleVideoSuccess"
            action="https://www.watabe-wedding.com.cn/mini/api/upload/video"
          >
            <el-button size="small" type="primary" icon="el-icon-upload"
              >点击上传</el-button
            >
          </el-upload>
          <div
            style="display: flex; align-items: flex-end"
            v-show="ruleForm.video"
          >
            <video
              :src="ruleForm.video"
              controls
              width="320"
              height="240"
            ></video>
            <el-button
              size="small"
              @click="handleVideoRemove"
              icon="el-icon-delete"
              style="margin-left: 8px"
              >删除视频</el-button
            >
          </div>
        </el-form-item>
      </el-card>
      <br />
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>轮播图</span>
          </div>
        </template>
        <el-form-item label="标题" prop="lunbo_title">
          <el-input type="text" v-model="ruleForm.lunbo_title"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="lunbo_description">
          <el-input
            type="textarea"
            v-model="ruleForm.lunbo_description"
          ></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="imgs">
          <el-upload
            action="https://www.watabe-wedding.com.cn/mini/api/upload/img"
            list-type="picture-card"
            :before-upload="handleBeforeUpload"
            :show-file-list="false"
            :on-success="(e) => handleUrlsSuccess(e, 'imgs')"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <div
            v-if="ruleForm.imgs.length"
            style="display: flex; flex-wrap: wrap"
          >
            <div
              v-for="(item, index) in ruleForm.imgs"
              :key="item"
              style="display: flex; flex-direction: column; align-items: center"
            >
              <el-image
                style="
                  width: 120px;
                  height: 120px;
                  border: 1px solid #e9e9e9;
                  padding: 8px;
                "
                :src="item"
                class="avatar"
                :preview-src-list="ruleForm.imgs"
              >
              </el-image>
              <el-button
                @click="handleRemove(index, 'imgs')"
                style="margin: 8px"
                type="primary"
                icon="el-icon-delete"
              ></el-button>
            </div>
          </div>
        </el-form-item>
      </el-card>
      <br />
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>婚礼图</span>
          </div>
        </template>
        <el-form-item label="标题" prop="wedding_title">
          <el-input type="text" v-model="ruleForm.wedding_title"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="wedding_description">
          <el-input
            type="textarea"
            v-model="ruleForm.wedding_description"
          ></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="wedding_imgs">
          <el-upload
            action="https://www.watabe-wedding.com.cn/mini/api/upload/img"
            list-type="picture-card"
            :before-upload="handleBeforeUpload"
            :show-file-list="false"
            :on-success="(e) => handleUrlsSuccess(e, 'wedding_imgs')"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <div
            v-if="ruleForm.wedding_imgs.length"
            style="display: flex; flex-wrap: wrap"
          >
            <div
              v-for="(item, index) in ruleForm.wedding_imgs"
              :key="item"
              style="display: flex; flex-direction: column; align-items: center"
            >
              <el-image
                style="
                  width: 120px;
                  height: 120px;
                  border: 1px solid #e9e9e9;
                  padding: 8px;
                "
                :src="item"
                class="avatar"
                :preview-src-list="ruleForm.wedding_imgs"
              >
              </el-image>
              <el-button
                @click="handleRemove(index, 'wedding_imgs')"
                style="margin: 8px"
                type="primary"
                icon="el-icon-delete"
              ></el-button>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="滑动图片" prop="wedding_imgs2">
          <el-upload
            action="https://www.watabe-wedding.com.cn/mini/api/upload/img"
            list-type="picture-card"
            :before-upload="handleBeforeUpload"
            :show-file-list="false"
            :on-success="(e) => handleUrlsSuccess(e, 'wedding_imgs2')"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <div
            v-if="ruleForm.wedding_imgs2.length"
            style="display: flex; flex-wrap: wrap"
          >
            <div
              v-for="(item, index) in ruleForm.wedding_imgs2"
              :key="item"
              style="display: flex; flex-direction: column; align-items: center"
            >
              <el-image
                style="
                  width: 120px;
                  height: 120px;
                  border: 1px solid #e9e9e9;
                  padding: 8px;
                "
                :src="item"
                class="avatar"
                :preview-src-list="ruleForm.wedding_imgs2"
              >
              </el-image>
              <el-button
                @click="handleRemove(index, 'wedding_imgs2')"
                style="margin: 8px"
                type="primary"
                icon="el-icon-delete"
              ></el-button>
            </div>
          </div>
        </el-form-item>
      </el-card>
      <br />
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>晚宴图</span>
          </div>
        </template>
        <el-form-item label="标题" prop="wanyan_title">
          <el-input type="text" v-model="ruleForm.wanyan_title"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="wanyan_description">
          <el-input
            type="textarea"
            v-model="ruleForm.wanyan_description"
          ></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="wanyan_imgs">
          <el-upload
            action="https://www.watabe-wedding.com.cn/mini/api/upload/img"
            list-type="picture-card"
            :before-upload="handleBeforeUpload"
            :show-file-list="false"
            :on-success="(e) => handleUrlsSuccess(e, 'wanyan_imgs')"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <div
            v-if="ruleForm.wanyan_imgs.length"
            style="display: flex; flex-wrap: wrap"
          >
            <div
              v-for="(item, index) in ruleForm.wanyan_imgs"
              :key="item"
              style="display: flex; flex-direction: column; align-items: center"
            >
              <el-image
                style="
                  width: 120px;
                  height: 120px;
                  border: 1px solid #e9e9e9;
                  padding: 8px;
                "
                :src="item"
                class="avatar"
                :preview-src-list="ruleForm.wanyan_imgs"
              >
              </el-image>
              <el-button
                @click="handleRemove(index, 'wanyan_imgs')"
                style="margin: 8px"
                type="primary"
                icon="el-icon-delete"
              ></el-button>
            </div>
          </div>
        </el-form-item>

        <el-form-item label="滑动图片" prop="wanyan_imgs2">
          <el-upload
            action="https://www.watabe-wedding.com.cn/mini/api/upload/img"
            list-type="picture-card"
            :before-upload="handleBeforeUpload"
            :show-file-list="false"
            :on-success="(e) => handleUrlsSuccess(e, 'wanyan_imgs2')"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <div
            v-if="ruleForm.wanyan_imgs2.length"
            style="display: flex; flex-wrap: wrap"
          >
            <div
              v-for="(item, index) in ruleForm.wanyan_imgs2"
              :key="item"
              style="display: flex; flex-direction: column; align-items: center"
            >
              <el-image
                style="
                  width: 120px;
                  height: 120px;
                  border: 1px solid #e9e9e9;
                  padding: 8px;
                "
                :src="item"
                class="avatar"
                :preview-src-list="ruleForm.wanyan_imgs2"
              >
              </el-image>
              <el-button
                @click="handleRemove(index, 'wanyan_imgs2')"
                style="margin: 8px"
                type="primary"
                icon="el-icon-delete"
              ></el-button>
            </div>
          </div>
        </el-form-item>
      </el-card>
      <br />
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>酒店图</span>
          </div>
        </template>
        <el-form-item label="上面图片" prop="hotel_imgs_top">
          <el-upload
            action="https://www.watabe-wedding.com.cn/mini/api/upload/img"
            list-type="picture-card"
            :before-upload="handleBeforeUpload"
            :show-file-list="false"
            :on-success="(e) => handleUrlsSuccess(e, 'hotel_imgs_top')"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <div
            v-if="ruleForm.hotel_imgs_top.length"
            style="display: flex; flex-wrap: wrap"
          >
            <div
              v-for="(item, index) in ruleForm.hotel_imgs_top"
              :key="item"
              style="display: flex; flex-direction: column; align-items: center"
            >
              <el-image
                style="
                  width: 120px;
                  height: 120px;
                  border: 1px solid #e9e9e9;
                  padding: 8px;
                "
                :src="item"
                class="avatar"
                :preview-src-list="ruleForm.hotel_imgs_top"
              >
              </el-image>
              <el-button
                @click="handleRemove(index, 'hotel_imgs_top')"
                style="margin: 8px"
                type="primary"
                icon="el-icon-delete"
              ></el-button>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="标题" prop="hotel_title">
          <el-input type="text" v-model="ruleForm.hotel_title"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="hotel_description">
          <el-input
            type="textarea"
            v-model="ruleForm.hotel_description"
          ></el-input>
        </el-form-item>
        <el-form-item label="跳转地址" prop="other_link">
          <el-input type="text" v-model="ruleForm.hotel_link"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="hotel_imgs">
          <el-upload
            action="https://www.watabe-wedding.com.cn/mini/api/upload/img"
            list-type="picture-card"
            :before-upload="handleBeforeUpload"
            :show-file-list="false"
            :on-success="(e) => handleUrlsSuccess(e, 'hotel_imgs')"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <div
            v-if="ruleForm.hotel_imgs.length"
            style="display: flex; flex-wrap: wrap"
          >
            <div
              v-for="(item, index) in ruleForm.hotel_imgs"
              :key="item"
              style="display: flex; flex-direction: column; align-items: center"
            >
              <el-image
                style="
                  width: 120px;
                  height: 120px;
                  border: 1px solid #e9e9e9;
                  padding: 8px;
                "
                :src="item"
                class="avatar"
                :preview-src-list="ruleForm.hotel_imgs"
              >
              </el-image>
              <el-button
                @click="handleRemove(index, 'hotel_imgs')"
                style="margin: 8px"
                type="primary"
                icon="el-icon-delete"
              ></el-button>
            </div>
          </div>
        </el-form-item>
      </el-card>
      <br />
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>跳转其他</span>
          </div>
        </template>
        <div v-for="(item, index) in ruleForm.other_imgs" :key="item">
          <el-form-item :label="`标题${index + 1}`" prop="title">
            <el-input
              type="text"
              v-model="ruleForm['other']['title' + index]"
            ></el-input>
          </el-form-item>
          <el-form-item :label="`描述${index + 1}`" prop="description">
            <el-input
              type="textarea"
              v-model="ruleForm['other']['description' + index]"
            ></el-input>
          </el-form-item>
          <el-form-item :label="`跳转地址${index + 1}`" prop="link">
            <el-input
              type="text"
              v-model="ruleForm['other']['link' + index]"
            ></el-input>
          </el-form-item>
        </div>

        <el-form-item label="图片" prop="other_imgs">
          <el-upload
            action="https://www.watabe-wedding.com.cn/mini/api/upload/img"
            list-type="picture-card"
            :before-upload="handleBeforeUpload"
            :show-file-list="false"
            :on-success="(e) => handleUrlsSuccess(e, 'other_imgs')"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <div
            v-if="ruleForm.other_imgs.length"
            style="display: flex; flex-wrap: wrap"
          >
            <div
              v-for="(item, index) in ruleForm.other_imgs"
              :key="item"
              style="display: flex; flex-direction: column; align-items: center"
            >
              <el-image
                style="
                  width: 120px;
                  height: 120px;
                  border: 1px solid #e9e9e9;
                  padding: 8px;
                "
                :src="item"
                class="avatar"
                :preview-src-list="ruleForm.other_imgs"
              >
              </el-image>
              <el-button
                @click="
                  handleRemove(index, 'other_imgs', {
                    obj: ruleForm['other'],
                    keys: ['title', 'description', 'link'],
                    index: index,
                  })
                "
                style="margin: 8px"
                type="primary"
                icon="el-icon-delete"
              ></el-button>
            </div>
          </div>
        </el-form-item>
      </el-card>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { reactive, ref, toRefs, nextTick } from "vue";
import axios from "axios";
import { parseTime } from "@/utils";
import { ElMessage } from "element-plus";
import { parseN, StrN } from "../utils/index";

const InitForm = {
  workName: "",
  cover: "",
  parentId: "",
  title: "",
  date: "",
  description: "",
  order: 1,
  bg: [],
  imgs: [],
  wedding_imgs: [],
  wedding_imgs2: [],
  wanyan_imgs: [],
  hotel_imgs: [],
  hotel_imgs_top: [],
  other_imgs: [],
  wanyan_imgs2: [],
  other: {},
};
export default {
  name: "DialogAddWork",
  props: {
    type: String,
    reload: Function,
    options: Object,
  },
  setup(props) {
    const formRef = ref();
    const state = reactive({
      cascaderProps: {
        value: "_id",
        label: "name",
      }, // 树形props
      // uploadImgServer,
      token: localStorage.access_token,
      visible: false,
      ruleForm: InitForm,
      rules: {
        workName: [
          { required: "true", message: "不能为空", trigger: ["change"] },
        ],
        cover: [{ required: "true", message: "不能为空", trigger: ["change"] }],
        description: [
          { required: "true", message: "不能为空", trigger: ["change"] },
        ],
        date: [{ required: "true", message: "不能为空", trigger: ["change"] }],
        order: [{ required: "true", message: "不能为空", trigger: ["change"] }],
        title: [{ required: "true", message: "不能为空", trigger: ["change"] }],
        phone: [{ required: "true", message: "不能为空", trigger: ["change"] }],
        bg: [{ required: "true", message: "不能为空", trigger: ["change"] }],
        video_title: [
          { required: "true", message: "不能为空", trigger: ["change"] },
        ],
        video_description: [
          { required: "true", message: "不能为空", trigger: ["change"] },
        ],
        parentId: [
          { required: "true", message: "不能为空", trigger: ["change"] },
        ],
      },
      id: "",
    });
    // 获取详情
    const getDetail = (id) => {
      axios.get(`/api/work/${id}`).then((res) => {
        console.log(res);
        state.ruleForm = {
          ...InitForm,
          ...StrN(res.data),
        };
      });
    };
    const handleBeforeUpload = (file) => {
      const sufix = file.name.split(".")[1] || "";
      if (!["jpg", "jpeg", "png"].includes(sufix)) {
        ElMessage.error("请上传 jpg、jpeg、png 格式的图片");
        return false;
      }
    };
    const handleVideoBeforeUpload = (file) => {
      // const sufix = file.name.split(".")[1] || "";
      // if (!["mp4"].includes(sufix)) {
      //   ElMessage.error("请上传 mp4 格式的视频");
      //   return false;
      // }
    };
    // 上传图片
    const handleUrlSuccess = (val) => {
      console.log("handleUrlSuccess", val);
      state.ruleForm.cover = val || "";
    };
    // 上传图片列表
    const handleUrlsSuccess = (val, key) => {
      console.log(val);
      state.ruleForm[key] = [...state.ruleForm[key], val];
    };
    // 上传视频
    const handleVideoSuccess = (val) => {
      console.log(val);
      state.ruleForm.video = val || "";
    };
    // 图片列表删除
    const handleRemove = (index, key, json) => {
      state.ruleForm[key].splice(index, 1);
      if (json) {
        if (state.ruleForm[key].length == 0) {
          for (let k in json["obj"]) {
            delete json["obj"][k];
          }
        } else {
          json.keys.map((item) => {
            delete json["obj"][item + json["index"]];
          });
        }
      }
    };
    // 视频删除
    const handleVideoRemove = () => {
      state.ruleForm.video = "";
    };

    // 开启弹窗
    const open = (id) => {
      state.visible = true;
      if (id) {
        state.id = id;
        getDetail(id);
      } else {
        state.ruleForm = InitForm;
      }
    };
    // 关闭弹窗
    const close = () => {
      state.visible = false;
    };
    const submitForm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          // if (hasEmoji(state.ruleForm.link)) {
          //   ElMessage.error('不要输入表情包，再输入就打死你个龟孙儿~')
          //   return
          // }
          state.ruleForm.date = parseTime(state.ruleForm.date);
          if (props.type == "add") {
            axios({
              method: "post",
              url: "/api/work",
              data: {
                ...parseN(state.ruleForm),
              },
            }).then((res) => {
              console.log(res);
              ElMessage.success("添加成功");
              state.visible = false;
              if (props.reload) props.reload();
            });
          } else {
            axios
              .put("/api/work", {
                ...parseN(state.ruleForm),
              })
              .then(() => {
                ElMessage.success("修改成功");
                state.visible = false;
                if (props.reload) props.reload();
              });
          }
        }
      });
    };
    const resetForm = () => {
      nextTick(() => {
        formRef.value.resetFields();
      });
    };
    return {
      ...toRefs(state),
      open,
      close,
      formRef,
      handleBeforeUpload,
      handleVideoBeforeUpload,
      handleUrlSuccess,
      handleUrlsSuccess,
      handleVideoSuccess,
      handleRemove,
      handleVideoRemove,
      submitForm,
      resetForm,
    };
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader {
  width: 100px;
  height: 100px;
  color: #ddd;
  font-size: 30px;
}
.avatar-uploader-icon {
  display: block;
  width: 100%;
  height: 100%;
  border: 1px solid #e9e9e9;
  padding: 32px 17px;
}
</style>
